package com.glls.customer.config;

import org.springframework.cloud.loadbalancer.core.ReactorServiceInstanceLoadBalancer;
import org.springframework.cloud.loadbalancer.core.RoundRobinLoadBalancer;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class CustomLoadBalancerConfig {


    @Bean
    public ReactorServiceInstanceLoadBalancer reactorServiceInstanceLoadBalancer(
            Environment environment, LoadBalancerClientFactory loadBalancerClientFactory) {
        String serviceId = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);

        //轮询
        return new RoundRobinLoadBalancer(
                loadBalancerClientFactory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class),
                serviceId);

        //随机
        //return new RandomLoadBalancer(
        //        loadBalancerClientFactory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class),
        //        serviceId);

        //自定义
        //return new CustomLoadBalancer(
        //        loadBalancerClientFactory.getLazyProvider(serviceId, ServiceInstanceListSupplier.class),serviceId
        //        );

    }



}
